package com.atguigu.tingshu.order.strategy.Impl;

import com.atguigu.tingshu.account.client.UserAccountFeignClient;
import com.atguigu.tingshu.common.constant.KafkaConstant;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.result.ResultCodeEnum;
import com.atguigu.tingshu.common.service.KafkaService;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.order.service.OrderDerateService;
import com.atguigu.tingshu.order.service.OrderDetailService;
import com.atguigu.tingshu.order.service.OrderInfoService;
import com.atguigu.tingshu.order.strategy.SaveOrderStrategy;
import com.atguigu.tingshu.vo.account.AccountLockResultVo;
import com.atguigu.tingshu.vo.account.AccountLockVo;
import com.atguigu.tingshu.vo.order.OrderInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class  AccountSaveOrderStrategy implements SaveOrderStrategy {

    @Autowired
    private OrderInfoService orderInfoService ;

    @Autowired
    private OrderDetailService orderDetailService ;

    @Autowired
    private OrderDerateService orderDerateService ;


    @Autowired
    private UserAccountFeignClient accountFeignClient;

    @Autowired
    private KafkaService kafkaService;

    @Override
    public OrderInfoVo saveOrder(OrderInfoVo orderInfoVo) {

        // 获取当前登录用户的id
        Long userId = AuthContextHolder.getUserId();

        AccountLockVo accountLockVo = new AccountLockVo() ;
        accountLockVo.setOrderNo(orderInfoVo.getTradeNo());
        accountLockVo.setUserId(userId);
        accountLockVo.setAmount(orderInfoVo.getOrderAmount());
        accountLockVo.setContent(orderInfoVo.getOrderDetailVoList().get(0).getItemName());
        Result<AccountLockResultVo> checkedAndLockAccount = accountFeignClient.checkAndLockAccount(accountLockVo);
        Integer code = checkedAndLockAccount.getCode();
        if(code.intValue() != 200) {
            log.info("账户金额锁定失败了了...失败原因：{}" , checkedAndLockAccount.getMessage() );
            throw new GuiguException(code , checkedAndLockAccount.getMessage()) ;
        }

        try {

            // 保存订单相关数据(保存订单的基本数据、保存订单的明细数据、保存订单的减免明细数据)
            this.saveOrderInfoAndDrateInfoAndDetailInfo(orderInfoVo , orderInfoService , orderDerateService , orderDetailService);

        } catch (Exception e) {
            e.printStackTrace();
            kafkaService.sendMsg(KafkaConstant.QUEUE_ACCOUNT_UNLOCK , orderInfoVo.getTradeNo());        // 发送消息
            throw new GuiguException(ResultCodeEnum.DATA_ERROR) ;
        }
        return orderInfoVo;
    }

    @Override
    public String getPayWay() {
        return SystemConstant.ORDER_PAY_ACCOUNT;
    }

    @Override
    public void saveOrderProcess(String orderNo) {
        // 保存完订单数据以后，执行后续操作
        orderInfoService.orderPaySuccess(orderNo);

        // 扣减账户的总金额
        kafkaService.sendMsg(KafkaConstant.QUEUE_ACCOUNT_MINUS , orderNo);
    }

}
